package com.scanner.hardware;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scanner.barcodeservice.SerialPortService;
import com.scanner.base.BaseActivity;
import com.scanner.bean.CodeMulit;
import com.scanner.bean.CodeMulitContent;
import com.scanner.bean.MotoBarcode;
import com.scanner.bean.MotoQRBarcode;
import com.scanner.hardware.adapter.MotoAllCodeAdapter;
import com.scanner.hardware.db.helper.DaoHelper;
import com.scanner.ui.RecycleViewDivider;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MotoQRcodeActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    private DaoHelper daoHelper;
    private List<MotoQRBarcode> qrMotoBars;

    @Override
    protected int getLayout() {
        return R.layout.activity_moto_qrcode;
    }

    @Override
    protected void initView() {
        initData();
        tvTitle.setText(getString(R.string.motorolatwo));
        llBack.setVisibility(View.VISIBLE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        MotoAllCodeAdapter codeAdapter = new MotoAllCodeAdapter(list, this);
        rvList.setAdapter(codeAdapter);
        codeAdapter.expandAll();
       /* MotoQRAdapter adapter = new MotoQRAdapter(R.layout.item_motosiger_layout,qrMotoBars);
        rvList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                int codeState = qrMotoBars.get(position).getQRcodeState();
                if(codeState == 1){//打开
                    qrMotoBars.get(position).setQRcodeState(0);
                    sendBrocastUpdate(qrMotoBars.get(position).getQRcodeCloseValue());
                }else if(codeState == 0){//关闭
                    qrMotoBars.get(position).setQRcodeState(1);
                    sendBrocastUpdate(qrMotoBars.get(position).getQRcodeOpenValue());
                }
                daoHelper.upDateQRCheck(qrMotoBars.get(position));
                adapter.setData(position,qrMotoBars.get(position));
            }
        });*/
    }

    private List<MultiItemEntity> list = new ArrayList<>();

    private void initData() {
        if (list.size() > 0) {
            list.clear();
        }
        daoHelper = new DaoHelper(this);
        qrMotoBars = daoHelper.getQRMotoBars();
        List<MotoBarcode> motoBars = daoHelper.getMotoBars();
        CodeMulit mulit1 = new CodeMulit();
        mulit1.setTitle(getString(R.string.str_module_sig));
        for (int i = 0; i < motoBars.size(); i++) {
            CodeMulitContent codeMulitContent = new CodeMulitContent();
            codeMulitContent.setCodeName(motoBars.get(i).getCodeName());
            codeMulitContent.setCodeClose(motoBars.get(i).getCodeCloseValue());
            codeMulitContent.setCodeOpen(motoBars.get(i).getCodeOpenValue());
            codeMulitContent.setCodeState(motoBars.get(i).getCodeState());
            mulit1.addSubItem(codeMulitContent);
        }
        list.add(mulit1);
        CodeMulit mulit2 = new CodeMulit();
        mulit2.setTitle(getString(R.string.str_module_qr));
        for (int i = 0; i < qrMotoBars.size(); i++) {
            CodeMulitContent codeMulitContent = new CodeMulitContent();
            codeMulitContent.setCodeName(qrMotoBars.get(i).getQRcodeName());
            codeMulitContent.setCodeClose(qrMotoBars.get(i).getQRcodeCloseValue());
            codeMulitContent.setCodeOpen(qrMotoBars.get(i).getQRcodeOpenValue());
            codeMulitContent.setCodeState(qrMotoBars.get(i).getQRcodeState());
            mulit2.addSubItem(codeMulitContent);
        }
        list.add(mulit2);
    }

    private void sendBrocastUpdate(byte[] value) {
        Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
        intent.putExtra(SerialPortService.BARCODEPORT_WRITER_WXTAR_BYTRDATA, value);
        sendBroadcast(intent);
    }


    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        finish();
    }
}
